# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=rasqal
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.9.33
pkgrel=2
pkgdesc="RDF Query Library. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://librdf.org/rasqal"
msys2_repository_url="https://github.com/dajobe/rasqal"
msys2_references=(
  'archlinux: rasqal'
)
license=('spdx:Apache-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-raptor")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc")
source=("http://download.librdf.org/source/rasqal-${pkgver}.tar.gz"
        "https://github.com/dajobe/rasqal/commit/4a2e3d2b866a1400eebb5110e95aedc9bf372c39.patch"
        "001-fix-build-with-mingw.patch")
sha256sums=('6924c9ac6570bd241a9669f83b467c728a322470bf34f4b2da4f69492ccfd97c'
            '329c752fcb3576e1e30c07d6a8384876ae9d9ac258477cc71b55873f8f95a605'
            '4988b690fefdde1325a70e8aa9157995cb8fda3b258be9ff3ca903e3b4bfbc4e')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  # https://github.com/dajobe/rasqal/issues/12
  patch -p1 -i "${srcdir}"/4a2e3d2b866a1400eebb5110e95aedc9bf372c39.patch
  patch -p1 -i "${srcdir}"/001-fix-build-with-mingw.patch

  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--enable-release")
  else
    _extra_config+=("--enable-debug")
  fi

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --disable-static \
    --with-regex-library=pcre2 \
    "${_extra_config[@]}"

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  make check || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" make install

  sed -i -e "14s|${MINGW_PREFIX}|\$\{prefix\}|g" "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/rasqal.pc"

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LIB" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE-2.0.txt" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
